package com.ctgu.springshiro.utils;

import com.ctgu.springshiro.entity.ItemKillSuccess;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.cglib.beans.BeanMap;

public class DefineSqlProvider implements ProviderMethodResolver {

    public static String insertSelective(final ItemKillSuccess record) {
        BeanMap beanMap = BeanMap.create(record);
        return new SQL() {{
            INSERT_INTO("item_kill_success");
            for (Object key : beanMap.keySet()) {
                if (beanMap.get(key) != null) {
                    VALUES(getColumn(key.toString()), "#{" + key.toString() + "}");
                }
            }
        }}.toString();
    }

    private static String getColumn(String input) {
        StringBuilder sb = new StringBuilder();
        for (int i = input.length() - 1; i >= 0; i--) {
            char c = input.charAt(i);
            sb.append(c);
            if (c > 64 && c < 91 && i > 0) {
                sb.append("_");
            }
        }
        return sb.reverse().toString().toLowerCase();
    }

}